﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using static WriteBugs.Controls.Utils;

namespace WriteBugs.Controls
{
    public class DragTitleAdorner : Adorner
    {
        private readonly ContentPresenter _contentPresenter;
        private Control Control
        {
            get
            {
                return (Control)this.AdornedElement;
            }
        }

        public DragTitleAdorner(UIElement adornedElement, Point pos, string? Title = "") : base(adornedElement)
        {
            IsHitTestVisible = false;

            int width = 22;
            if (Title != null)
            {
                width += (int)MeasureTextWidth(Title, 14, "宋体");
            }

            this._contentPresenter = new ContentPresenter
            {
                Content = new Border
                {
                    Background = Brushes.SteelBlue,
                    Width = width,
                    Height = 28,
                    BorderBrush = Brushes.Gray,
                    BorderThickness = new Thickness(1),
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top,
                    CornerRadius= new CornerRadius(5),
                    Child = new TextBlock
                    {
                        Text = Title,
                        FontSize = 14,
                        FontFamily= new FontFamily("宋体"),
                        Foreground = Brushes.White,
                        HorizontalAlignment = HorizontalAlignment.Left,
                        VerticalAlignment = VerticalAlignment.Center,
                        Margin = new Thickness(10, 0, 0, 0),
                    },
                },
            };

            double left = pos.X;
            double top = pos.Y;
            this.Margin = new Thickness(left + 5, top + 10, 0, 0);
        }

        #region Override

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        protected override Visual GetVisualChild(int index) // replace the Visual of the TextBox with the visual of the _contentPresenter;
        {
            return this._contentPresenter;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            this._contentPresenter.Measure(this.Control.RenderSize); // delegate the measure override to the ContentPresenter's Measure
            return this.Control.RenderSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            this._contentPresenter.Arrange(new Rect(finalSize));
            return finalSize;
        }

        #endregion Override

        private  double MeasureTextWidth(string text, double fontSize, string fontFamily)
        {
            FormattedText formattedText = new FormattedText(
            text,
            System.Globalization.CultureInfo.InvariantCulture,
            FlowDirection.LeftToRight,
            new Typeface(fontFamily.ToString()),
            fontSize,
            Brushes.Black
            );
            return formattedText.WidthIncludingTrailingWhitespace;
        }

    }
}
